package com.inossem.javacvdemo;
//javacv把海康摄像头rtsp流转推到rtmp流
//https://blog.csdn.net/leeking888/article/details/77542661

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;
public class TestRtsp2Rtmp {
	static boolean exit  = false;
	public static void main(String[] args) throws Exception {
		System.out.println("start...");
		String rtmpPath = "rtmp://localhost:19350/hls/test01";
		//String rtmpPath = "rtmp://58.200.131.2:1935/livetv/hunantv";
		String rtspPath = "rtmp://58.200.131.2:1935/livetv/hunantv"; // 香港收视
		//String rtspPath = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
		//String rtspPath = "rtsp://admin:123123@192.168.1.64:554/h264/ch1/main/av_stream";
		//ffmpeg -f rtsp -rtsp_transport tcp -i rtsp://admin:leeking123@192.168.1.64:554/h264/ch1/main/av_stream rtmp://casic207-pc1/live360p/ss1
		// ffmpeg -i  rtsp://admin:123123@192.168.1.64:554/h264/ch1/main/av_stream -vcodec copy -acodec copy -f flv rtmp://casic207-pc1/live360p/ss1
		int audioRecord =0; // 0 = 不录制，1=录制
		boolean saveVideo = false;
		test(rtmpPath,rtspPath,audioRecord,saveVideo);
		System.out.println("end...");
	}
 
	public static void test(String rtmpPath,String rtspPath,int audioRecord,boolean saveVideo ) throws Exception  {
		//FrameGrabber grabber = FrameGrabber.createDefault(0); // 本机摄像头 默认
		// 使用rtsp的时候需要使用 FFmpegFrameGrabber，不能再用 FrameGrabber
		//int width = 1024,height = 576; //延时7
		int width = 1024,height = 576;
		FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(rtspPath); 
		grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式，不然会丢包很严重
		// 一直报错的原因！！！就是因为是 2560 * 1440的太大了。。
		grabber.setImageWidth(width);
		grabber.setImageHeight(height);
		System.out.println("grabber start");
		grabber.start();
		//FrameRecorder recorder = FrameRecorder.createDefault(rtmpPath, 640,480,0);
		// 流媒体输出地址，分辨率（长，高），是否录制音频（0:不录制/1:录制）
		FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(rtmpPath, width, height, audioRecord);
		recorder.setInterleaved(true);
        // 降低编码延时
        recorder.setVideoOption("tune", "zerolatency");
        // 提升编码速度
        recorder.setVideoOption("preset", "ultrafast");
        // 视频质量参数(详见 https://trac.ffmpeg.org/wiki/Encode/H.264)
        recorder.setVideoOption("crf", "28");

		//recorder.setVideoOption("crf","28");
		recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); // 28
		recorder.setFormat("flv"); // rtmp的类型
		recorder.setFrameRate(25);
	 	recorder.setPixelFormat(0); // yuv420p
	 	System.out.println("recorder start");
		recorder.start();
		//
		OpenCVFrameConverter.ToIplImage conveter = new OpenCVFrameConverter.ToIplImage();
		System.out.println("all start!!");
		int count = 0;
		while(!exit){
			count++;
			Frame frame = grabber.grabImage();
			if(frame == null){
				continue;
			}
			if(count % 50 == 0){
				System.out.println("count="+count);
			}
			recorder.record(frame); 
		}
		grabber.stop();
		grabber.release();
		recorder.stop();
		recorder.release();
	}
}